package com.renwei.yunlong.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.contacts.CompanySendInvitationActivity;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.Company;
import com.renwei.yunlong.bean.UserEnterpriseBean;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CompanyInfoActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {


    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;
    @Bind(R.id.tv_numbering)
    TextView tvNumbering;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_mail)
    TextView tvMail;
    @Bind(R.id.tv_contact)
    TextView tvContact;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.btn_chat)
    Button btnChat;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    private Company company;
    private String companyCode;
    private boolean isAdmin = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_info);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        toolbar.setTitle("");
        tvTitle.setText("企业信息");
        toolbar.setNavigationIcon(R.mipmap.back_icon);
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnChat.setOnClickListener(this);
        company = (Company) getIntent().getSerializableExtra("company");
        companyCode = getIntent().getStringExtra("companyCode");
        if ("1".equals(companyType)) {
            isAdmin = AppHelper.isAdminDesk(ownerBean);
        } else {
            isAdmin = AppHelper.isAdminDesk(serviceLoginBean);
        }
        initdata();
    }

    public static void OpenActivity(Context context, Company company, String companyCode) {
        Intent intent = new Intent(context, CompanyInfoActivity.class);
        intent.putExtra("company", company);
        intent.putExtra("companyCode", companyCode);
        context.startActivity(intent);
    }


    private void initdata() {
        if (company != null) {
            btnChat.setVisibility(View.VISIBLE);

            tvCompanyName.setText(company.getCompanyName());
            tvNumbering.setText(company.getCompanyCode());
            if ("1".equals(company.getType())) {
                tvType.setText("/" + "业主");
            } else {
                tvType.setText("/" + "服务端");
            }
            tvMail.setText(StringUtils.value(company.getMail()));//邮箱
            tvContact.setText(company.getRegisterName());//联系人
            tvPhone.setText(company.getCustomerPhone());//电话
            tvAddress.setText(String.valueOf(company.getProvinceId() + company.getCityId() + company.getRegionId() + company.getRegisterAddr()));

        } else {
            btnChat.setVisibility(View.GONE);

            HashMap<String, String> map = new HashMap<>();
            map.put("companyCode", companyCode);
            if ("1".equals(companyType)) {
                map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            } else {
                map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            }
            ServiceRequestManager.getManager().QueryUserCompany(this, JsonMapUtil.mapToJson(map), this);
        }


    }

    @Override
    public String getCompanyCode() {
        String companyCode = "";
        if ("1".equals(companyType)) {
            companyCode = StringUtils.value(ownerBean.getRows().getCompany().getCompanyCode());
        } else if ("2".equals(companyType)) {
            companyCode = StringUtils.value(serviceLoginBean.getRows().getCompany().getCompanyCode());
        } else {
            LogUtil.e("公司数据错误");
            return "";
        }
        return companyCode;
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_chat:
                if (isAdmin) {
                    LogUtil.e("isadminordesk+yaoqin"+isAdmin);
                    this.finish();
                    CompanySendInvitationActivity.OpenActivity(this, company.getCompanyName(), company.getCompanyCode());
                } else {
                    showCenterInfoMsg("只有管理员有权限添加企业好友");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 880:
                UserEnterpriseBean userEnterpriseBean = new Gson().fromJson(result, UserEnterpriseBean.class);
                if (userEnterpriseBean.getMessage().getCode() == 200) {

                    UserEnterpriseBean.RowsBean bean = userEnterpriseBean.getRows();
                    if ("1".equals(bean.getType())) {
                        tvType.setText("/" + "业主");
                    } else {
                        tvType.setText("/" + "服务端");
                    }
                    tvCompanyName.setText(bean.getServerName());
                    tvNumbering.setText(bean.getCompanyCode());
//                    tvMail.setText(StringUtils.value(bean.g));
                    tvContact.setText(bean.getRegisterName());//联系人
                    tvPhone.setText(bean.getCustomerPhone());//电话
                    tvAddress.setText(bean.getRegisterAddr());
//
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
    }
}
